package com.itheima.behaviour.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.behaviour.dto.FollowBehaviourDto;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class FollowBehaviourListener {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = BusinessConstants.MqConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void saveFollowBehaviour(ConsumerRecord<String,String> record){
        log.info("用户关注行为 开始：topic={},partition={},msg={}",record.topic(),
                record.partition(),record.value());
        //解析内容，转为dto
        FollowBehaviourDto followBehaviourDto = JSON.parseObject(record.value(), FollowBehaviourDto.class);

        //保存用户的行为
        apFollowBehaviorService.saveFollowBehaviour(followBehaviourDto);
        log.info("用户关注行为 结束：topic={},partition={},msg={}",record.topic(),
                record.partition(),record.value());

    }
}
